package com.zui.webapp.action;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.zui.entity.BaseEntity;
import com.zui.entity.product.Geo;
import com.zui.manager.GeoManager;
import com.zui.manager.SessionManager;
import com.zui.util.JqGridPage;

public class GeoAction extends BaseJQGridAction {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(GeoAction.class);
	
	private Geo geo;
	private List<Geo> list;
	public GeoAction() {}
	public GeoAction(SessionManager<Geo, Long> sessionManager) {
		this.sessionManager = sessionManager;
	}
	
	@Autowired
	private SessionManager<Geo, Long> sessionManager;
	@Autowired
	private GeoManager geoManager;
	/***********************************************************
	 * Business Method start
	 **********************************************************/
	public String getDoubleSelectDataByGeoId() {
		if (geo == null || geo.getId() == null)
			return "{}";
		geo = sessionManager.loadById(Geo.class, geo.getId());
		jsonResult = geo.getDoubleSelectJson();
		return "jsonResult";
	}
	/**
	 * Get all available geos by geoTypeId
	 * @return all geos match the specific geo type order by name asc.
	 */
	public String getGeosByGeoTypeId() {
		if (geo == null || geo.getGeoType() == null || geo.getGeoType().getId().trim().equals(""))
			return "{}";
		
		list = geoManager.findGeosByGeoTypeId(geo.getGeoType().getId());
		return "jsonList";
	}
	/**
	 * Get all children by geo id and geo type
	 * @return all children geos order by name asc;
	 */
	public String getChildrenGeosByGeoId() {
		if (geo == null || geo.getId() == null)
			return "{}";
		geo = sessionManager.loadById(Geo.class, geo.getId());
		if (list == null)
			list = new ArrayList<Geo>();
		list.addAll(geo.getChildrenGeos());
		return "jsonList";
	}
	public String index() {return "index";}
	public String editNew() {
		geo = new Geo();
		return "editNew";
	}
	public String edit() {
		if (geo.getId()==null)
			return "editNew";
		
		geo = (Geo)sessionManager.loadById(Geo.class, geo.getId());
		return "edit";
	}
	public String view() {
		if (geo.getId()==null)
			return "view";
		geo = (Geo)sessionManager.loadById(Geo.class, geo.getId());
		return "view";
	}
	public String delete() {
		if (jq==null || jq.getId()==null ||jq.getId().trim().equals(""))
			return "index";
		sessionManager.deleteEngtityFlagByIds(Geo.class, jq.getId().split("\\,"));
		/**
		 * @TODO: success message!
		 */
		return "index";
	}
	public String checkUniqueness() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(Geo.class);
		if (checkedField!=null && !"".equals(checkedField.trim())) {
			
			String methodName = "get" + checkedField.substring(0, 1).toUpperCase() + checkedField.substring(1);
			Method method = Geo.class.getMethod(methodName);
			criteria.add(Restrictions.eq(checkedField, method.invoke(geo)));
			valid = !sessionManager.checkExistence(criteria);
		}
		return "valid";
	}
	public String create() {
		geo.setCreatedByUserLogin(super.getCurrentUserName());
		geo.setCreatedDate(Calendar.getInstance().getTime());
		geo.setLastModifiedByUserLogin(super.getCurrentUserName());
		geo.setLastModifiedDate(Calendar.getInstance().getTime());
		sessionManager.saveEntity(geo);
		return "view";
	}
	public String update() {

		Geo tempGeo = geo;
		geo = sessionManager.loadById(Geo.class, geo.getId());
		geo.setName(tempGeo.getName());
		geo.setNameEn(tempGeo.getNameEn());
		geo.setDescription(tempGeo.getDescription());
		geo.setLastModifiedByUserLogin(super.getCurrentUserName());
		geo.setLastModifiedDate(Calendar.getInstance().getTime());
		sessionManager.saveOrUpdate(geo);
		return "view";
	}
	public String createAndNew() {
		this.create();
		geo = null;
		return "editNew";
	}
	public String duplicate() {
		if (geo.getId()==null)
			return "editNew";
		
		geo = (Geo)sessionManager.loadById(Geo.class, geo.getId());
		geo.setId(null);
		geo.setIsDeleted(BaseEntity.FLAG_N);
		return "editNew";
	}
	/**
	 * search the whole geo list
	 * @return
	 */
	public String geoJsonList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Geo.class);
		if (jq !=null && jqFilters!=null) {
			jq.setGroupCriteria(criteria,jqFilters);
		}
		list = sessionManager.findByCriteriaForJQGrid(criteria,jq == null ? jq = new JqGridPage()
		: jq);
		
		//set null for session scope action
		jqFilters = null;
		return "geoJsonList";
	}
	public void jqgridOper() {
		switch(jq.getOper()) {
			case del: delete();break;
			case add: create();break;
			case edit: create();break;
			default:break;
		}
	}
	

	/***********************************************************
	 * getters and setters
	 **********************************************************/
	/**
	 * @return the userList
	 */
	public List<Geo> getlist() {
		return list;
	}
	/**
	 * @param userList the userList to set
	 */
	public void setList(List<Geo> list) {
		this.list = list;
	}
	/**
	 * @return the geo
	 */
	public Geo getGeo() {
		return geo;
	}
	/**
	 * @return the list
	 */
	public List<Geo> getList() {
		return list;
	}
	/**
	 * @param geo the geo to set
	 */
	public void setGeo(Geo geo) {
		this.geo = geo;
	}
	
}
